package com.controller.xsxt;

import com.entity.common.ApiResult;
import com.entity.xsxt.RoomShow;
import com.entity.xsxt.Student;
import com.entity.xsxt.VoteInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.xsxt.RoomManageService;
import com.service.xsxt.VoteInfoService;
import com.util.CommonUtil;
import com.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("rootManage")
public class RoomManageController {

    @Autowired
    private RoomManageService roomManageService;

    @Autowired
    private VoteInfoService voteInfoService;

    @RequestMapping("/add")
    public ApiResult add(@RequestParam("roomImg") MultipartFile roomImg, HttpServletRequest request,String roomNumber,String year){
        RoomShow roomShow = new RoomShow();
        roomShow.setRoomImg(FileUploadUtil.savaFile(roomImg,request));
        roomShow.setRoomNumber(roomNumber);
        roomShow.setYear(year==null?new SimpleDateFormat("yyyy").format(new Date()):year);
        roomManageService.insertSelective(roomShow);
        return new ApiResult(true,"新增成功");
    }

    @RequestMapping("/getAllData")
    public List<RoomShow> getAllData( String audit ){
        return roomManageService.getPageList(null,audit,null);
    }

    @RequestMapping("/deleteData")
    public ApiResult deleteData(int id ){
        return roomManageService.deleteData(id);
    }

    @RequestMapping("/updateData")
    public ApiResult updateData(RoomShow roomShow){
        roomShow.setAudit(true);
        return roomManageService.updateByPrimaryKeySelective(roomShow);
    }

    @RequestMapping("/vote")
    public ApiResult vote(VoteInfo record,HttpServletRequest request,HttpSession session){
        Student student= (Student) session.getAttribute("userInfo");
        record.setIpAddress(student.getStuCode());
        return voteInfoService.insertOrUpdate(record);
    }

    @RequestMapping("/getPageList")
    public Map<String, Object> getPageList(int pageNum,int pageSize,String flag){
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        String filter="filter";
        PageInfo<RoomShow> pageInfo = new PageInfo<>( roomManageService.getPageList(filter,null,flag));
        map.put("rows", pageInfo.getList());
        map.put("totalRows", pageInfo.getTotal());
        map.put("pageNum", pageNum);
        return map;
    }

}
